package com.ymf.twitter.action;

import java.util.Date;
import java.util.Map;

import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTablePool;

import com.opensymphony.xwork2.ActionContext;
import com.ymf.twitter.hbase.RelationsDAO;
import com.ymf.twitter.hbase.TwitsDAO;
import com.ymf.twitter.hbase.UsersDAO;
import com.ymf.twitter.model.User;

public class UserAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	String content;
	String email;
	String password;

	String password2;

	RelationsDAO relationsDao;
	TwitsDAO twitsDao;
	UsersDAO userDao;

	String username;

	public UserAction() {
		userDao = new UsersDAO(new HTablePool(HBaseConfiguration.create(), 10));
		twitsDao = new TwitsDAO(new HTablePool(HBaseConfiguration.create(), 10));
		relationsDao = new RelationsDAO(new HTablePool(
				HBaseConfiguration.create(), 10));
		username = new String();
		password = new String();

	}

	public String execute() throws Exception {
		return SUCCESS;
	}

	public String Follow() throws Exception {
		if (relationsDao.addFollowedBy(login_user, username))
			errmsg = "success";
		else
			errmsg = "error";

		return SUCCESS;
	}


	public String Login() throws Exception {
		User user = userDao.checkPassword(username, password);
		if (user != null) {
			login_user = username;
			// login_id = id;
			ActionContext actionContext = ActionContext.getContext();
			Map session = actionContext.getSession();
			session.put("login_user", login_user);
			session.put("login_id", login_id);
		} else {
			errmsg = "login error";
			return ERROR;
		}
		return SUCCESS;
	}

	public String Logout() throws Exception {
		login_user = "";
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();
		session.put("login_user", login_user);
		return SUCCESS;
	}

	public String Post() throws Exception {
		try{
			twitsDao.postTwit(login_user, new Date(), content);
			errmsg = "post success";
		}catch(Exception e){
			errmsg = "post error";
			throw e;
		}
		return SUCCESS;
	}

	public String Register() throws Exception {
		if (username.equals(""))
			return SUCCESS;

		if (username.length() < 4) {
			errmsg = "user name to short";
			return SUCCESS;
		}

		if (password.length() < 4) {
			errmsg = "password to short";
			return SUCCESS;
		}

		if (!password.equals(password2)) {
			errmsg = "password not same";
			return SUCCESS;
		}

		if (userDao.addUser(username, username, email, password)) {
			errmsg = "";
			return LOGIN;
		} else {
			errmsg = "";
			return SUCCESS;
		}
	}
	

	public String Unfollow() throws Exception {
		if (relationsDao.delFollowedBy(login_user, username))
			errmsg = "success";
		else
			errmsg = "error";

		return SUCCESS;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public void setUsername(String username) {
		this.username = username;
	}


	public String getContent() {
		return content;
	}

	public String getEmail() {
		return email;
	}

	public String getPassword() {
		return password;
	}

	public String getPassword2() {
		return password2;
	}

	public String getUsername() {
		return username;
	}
	
	public String SubmitPost() throws Exception {
		errmsg = "error";
		return SUCCESS;
	}

}